import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from '@/store'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: "/home"
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import( /* webpackChunkName: "about" */ '../views/Goods.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import( /* webpackChunkName: "about" */ '../views/User.vue'),
    redirect:"/user/cart",
    children:[
      {
        path:'cart',
        component: () => import( /* webpackChunkName: "about" */ '../components/user/Cart.vue'),
      }
    ]
    /* beforeEnter: (to, from, next) => {
      console.log('to1', to, 'from1', from);

      // 则需要获取是否存在token
      let token = localStorage.getItem('x-auth-token')
      if (token) {
        // 如果存在正常进入user页面
        next();
      } else {
        // 否则提示你还没有登录
        store.dispatch("showToast/asyncIsShowToast", {
          msg: "你还没有登录！",
          type: "warning",
        })
      }
    } */
  },

  {
    path: '/order',
    name: 'order',
    component: () => import( /* webpackChunkName: "about" */ '../views/Order.vue')
  },
  {
    path: '/free',
    name: 'free',
    component: () => import( /* webpackChunkName: "about" */ '../views/Free.vue')
  },
  {
    path: '/details',
    name: 'Details',
    component: () => import( /* webpackChunkName: "about" */ '../views/Details.vue')
  },
{
  // 匹配所有地址
  path:'*',
  name:'Error',
  component: () => import( /* webpackChunkName: "about" */ '../views/Error.vue')
}

  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => {})
};


// 导航守卫
// 全局的导航守卫
// to:Route:即将要进入的目标
// from:Route: 当前导航正要离开的路由
// next:Function : 一定要调用该方法来 resolve 这个例子
/* router.beforeEach((to,from,next)=>{

  // 如果你准备去user页面
  if(to.path == '/user'){
    // 则需要获取是否存在token
    let token = localStorage.getItem('x-auth-token')
    if(token){
      // 如果存在正常进入user页面
      next();
    }else{
      // 否则提示你还没有登录
      store.dispatch("showToast/asyncIsShowToast",{
        msg: "你还没有登录！",
        type: "warning",
    })
    }
    return
  }
  next();
}) */

export default router